package com.weme.live.activity.liveview

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.findViewTreeViewModelStoreOwner
import com.weme.common.util.clearText
import com.weme.live.databinding.ViewLiveRoomChatBinding
import com.weme.live.viewmode.LiveViewMode
import com.weme.common.util.setOnFastClickListener
import com.weme.common.util.UserMKHelper
import com.weme.common.util.showToast
import com.weme.live.activity.LiveRoomActivity

class LiveRoomChatView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : FrameLayout(context, attributeSet, defStyle) {
    private val viewBinding = ViewLiveRoomChatBinding.inflate(LayoutInflater.from(context), this, true)

    private var mLiveRoomVModel: LiveViewMode? = null

    private val MAX_CHARACTERS = 100 // 最大字符数量

    init {
        val activity = (context as LiveRoomActivity)
        val viewModel = ViewModelProvider((context as LiveRoomActivity)).get(LiveViewMode::class.java)
        viewModel.edTextView = viewBinding.edInput
        //发消息
        viewModel.sendMsgLiveData.observe(activity) {

            if(!TextUtils.isEmpty(viewBinding.edInput.text.toString().trim())){
                if(viewModel.isFictitious){
                    viewModel.sendMsgContentLiveData.postValue(Message(avatarUrl = "", nickname = "nickname", viewBinding.edInput.text.toString(), true))
                }else{
                    viewModel.anchorUser?.apply {
                        viewModel.sendMsgContentLiveData.postValue(Message(avatarUrl = headPic, nickname = nickname, viewBinding.edInput.text.toString(), true))
                    }
                }


                viewBinding.edInput.clearText()
            }

            //  mLiveRoomVModel?.textInputChangeLiveData?.postValue(false)

        }
        viewBinding.imOverturn.setOnFastClickListener {
            viewModel.switchCamera.postValue("")
        }

        viewBinding.imGift.setOnFastClickListener { //        val dialog = GiftDialog()
            //        dialog.show(this.supportFragmentManager,"giftDialog")
        }
        viewBinding.edInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (!s.isNullOrBlank() && s.length > MAX_CHARACTERS) {
                    // 如果输入字符数量超过最大限制，则截取前面的 MAX_CHARACTERS 个字符
                    val truncatedText = s.toString().substring(0, MAX_CHARACTERS)
                    // 更新输入框的文本
                    viewBinding.edInput.setText(truncatedText)
                    // 将光标移动到文本末尾
                    viewBinding.edInput.setSelection(MAX_CHARACTERS)
                    context.showToast(context.resources.getString(com.example.resources.R.string.live_max_characters_exceeded))
                }
                viewModel.setInputMsgLiveData.postValue(viewBinding.edInput.text.toString())

            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
    }

    fun getImOverturn(): View {
        return viewBinding.imOverturn
    }

    fun setViewMode(requireActivity: FragmentActivity) {
        mLiveRoomVModel = findViewTreeViewModelStoreOwner()?.let {
            ViewModelProvider(requireActivity)[LiveViewMode::class.java]
        }
    }
}